#coding:utf-8
'''
Created on 2017-04-20

@author: sandy
'''
import json
import os
import logging
from pycommon import Utils
from pycommon import RemoteManager


class Config(object):
    
    def __init__(self):
        self.rundir = os.path.dirname(__file__)
        self.server_ip = u"127.0.0.1"
        self.server_port = 1235
        self.public_ip = u"127.0.0.1"
        self.public_port = 1235
        self.web_port = 8088
        self.center_ip = u"127.0.0.1"
        self.center_port = 8000
        self.console_ip = u"127.0.0.1"
        self.console_port = 1236   
        self.max_client = 100
        
        self.redisdb_ip =u"192.168.1.100"
        self.redisdb_port=6379
        self.redisdb_db=0
        self.redisdb_passwd=u""

    
    def onRemoteConfig(self, cfgname, jf):
        ctype,cname = cfgname.split(u".")
        if ctype==u'server'+unicode(RemoteManager.instance.sid) and cname==u'serverconfig':
            self.loadFromJson(jf)
    
            
    def loadFromFile(self):
        cf = json.load(file(os.path.join(self.rundir  + "/../config/","gateconfig.json")), encoding='utf-8')
        self.loadFromJson(cf)
        
    def loadFromJson(self, cf):
        self.server_ip = cf.get("server_ip")
        self.server_port = cf.get("server_port")
        self.public_ip = cf.get("public_ip")
        self.public_port = cf.get("server_port")
        self.web_port = cf.get("web_port", 8088)
        self.center_ip = cf.get("center_ip")
        self.center_port = cf.get("center_port")    
        self.console_ip = cf.get("console_ip")
        self.console_port = cf.get("console_port")                 
        self.max_client = cf.get("max_client")

        dbstr = cf.get("redis_db")
        dbparams = dbstr.split(u',')
        self.redisdb_ip =dbparams[0]
        self.redisdb_port=int(dbparams[1])
        self.redisdb_db= int(dbparams[2])
        self.redisdb_passwd=dbparams[3] if dbparams[3]!="0" else None


instance = Config()
